package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ActivityListBean;
import com.cljz.makepassenger.model.net.bean.GetTypeListBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.NearbyActivity;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/26.
 */

public class NearbyP extends BaseP<NearbyActivity> {

    public void getTypeList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("type", "activity");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getTypeListData(requestBody)
                .compose(XApi.<GetTypeListBean>getApiTransformer())
                .compose(XApi.<GetTypeListBean>getScheduler())
                .compose(getV().<GetTypeListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetTypeListBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(GetTypeListBean getTypeListBean) {
                        if ("0".equals(getTypeListBean.getError_code())) {
                            getV().getTypeListSucceed(getTypeListBean.getData());
                        } else if ("1000".equals(getTypeListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTypeListFail(getTypeListBean.getMsg());
                        }
                    }
                });
    }

    public void getActivityList(int is_NearBy, int is_popular, int is_latest, int page, String key, double latitude, double longitude, String typeId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("is_nearby", String.valueOf(is_NearBy));
        paramsMap.put("is_popular", String.valueOf(is_popular));
        paramsMap.put("is_latest", String.valueOf(is_latest));
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("key", key);
        paramsMap.put("latitude", String.valueOf(latitude));
        paramsMap.put("longitude", String.valueOf(longitude));
        paramsMap.put("type_id", typeId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getActivityListData(requestBody)
                .compose(XApi.<ActivityListBean>getApiTransformer())
                .compose(XApi.<ActivityListBean>getScheduler())
                .compose(getV().<ActivityListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ActivityListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ActivityListBean activityListBean) {
                        if ("0".equals(activityListBean.getError_code())) {
                            getV().getActivityListSucceed(activityListBean.getData());
                        } else if ("1000".equals(activityListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getActivityListFail(activityListBean.getMsg());
                        }
                    }
                });
    }

    public void collection(String activityId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().collectionData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().collectionSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().collectionFail(responseBean.getMsg());
                        }
                    }
                });

    }

    public void collectionCancel(String activityId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().collectionCancelData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().collectionCancelSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().collectionCancelFail(responseBean.getMsg());
                        }
                    }
                });
    }
}
